﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MvcViewPage.Master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	PortfoliosEdit
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <link href="../../Content/Portfolios/PortfoliosEdit.css" rel="stylesheet" type="text/css" />
    <h2>PortfoliosEdit</h2>
    <link href="../../Content/Theme/ui.all.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    <script src="../../Scripts/ui/ui.core.js" type="text/javascript"></script>
    <script src="../../Scripts/ui/ui.datepicker.js" type="text/javascript"></script>
    <script type="text/javascript">
        var row_max = 7;
        var row_sort = 1;
        var sort_desc = false;
        //Load form
        $(document).ready(function(){
            //Load Drop down Port
            $.getJSON("/Portfolios/GetAllPort", null, function(data) {  
                for(var i in data)
                    $("<option value='" + data[i].Value + "'>" + data[i].Text + "</option>").appendTo("#DropPort");             
                $('#DropPort').change();
            });
            
            //khoi tao date time picker
            for(var i = 0; i < row_max; i++)
                $("#row" + i.toString() + "col3_edit").datepicker({ dateFormat: 'dd/mm/yy' });
        });
                    
        function DropPortChange(key) { 
            //lay port_id
            var port_id = $('#DropPort').val();
        
            //update lai Cashinfo
            $.getJSON("/Portfolios/GetPortInfo", { port_id: port_id}, function(data) {                
                $("#CashName").text(data.CashName);
                $("#AmountMoney").text(data.AmountMoney);
                
            });
            
            //update lai PortInfo de edit
            $.getJSON("/Portfolios/GetPortInfo_edit", { port_id: port_id}, function(data) {                
                $("#TB_tendanhmuc").val(data.PortfolioName);
                $("#TB_phigiaodich").val(data.Commission);
            });
            
            //update lai page_max
            $.getJSON("/Portfolios/GetMaxPage_PortTransaction", {port_id: port_id , row_max:row_max}, function(data) {                
                $("#page_max").text(data);
            });
            
                    
            //key : neu key = 0 ==> page = 1 **** key = 1 ==> page = page **** key = 2 pre *** key = 3 next
            var page;   
            switch(key)
            {
                case 0: page = 1; break;
                case 1: page = parseInt($("#page_current").text()); break;
                case 2: page = parseInt($("#page_current").text()) - 1; break;
                case 3: page = parseInt($("#page_current").text()) + 1; break;
            } 
                 
            if(page > $("#page_max").text())
                page = $("#page_max").text();
            if(page < 1)
                page = 1;
                         
            //update lai page_current
            $("#page_current").text(page);
        
            $.getJSON("/Portfolios/GetPortTransaction", {port_id: port_id, row_max:row_max, page:page, row_sort:row_sort,sort_desc:sort_desc}, function(data){                 
            
                //ghi du lieu moi - Chu y : phai chac chan cell nao cung co du lieu....de hien thi chinh xac table
                for(var i in data)   
                {               
                    if(i > row_max)
                        break;  
                    $("#row" + i.toString() + "col1_edit").val(data[i].Id);    
                        
                    $("#row" + i.toString() + "col1").text(data[i].StockSymbol);
                    if(data[i].Status == 1)
                        $("#row" + i.toString() + "col2").text("Mua");
                    else
                        $("#row" + i.toString() + "col2").text("Bán");
                    $("#row" + i.toString() + "col3").text(data[i].Date_str);
                    $("#row" + i.toString() + "col4").text(data[i].Amount);
                    $("#row" + i.toString() + "col5").text(data[i].Price);
                    if(data[i].Commission == null)
                        $("#row" + i.toString() + "col6").text("-");
                    else
                        $("#row" + i.toString() + "col6").text(data[i].Commission);
                    if(data[i].Note == null || data[i].Note == "")
                        $("#row" + i.toString() + "col7").text("-");
                    else
                        $("#row" + i.toString() + "col7").text(data[i].Note);
                    
                    //hide va show cac control va control edit
                    $("#row" + i.toString() + "col2_edit").hide();
                    $("#row" + i.toString() + "col3_edit").hide();
                    $("#row" + i.toString() + "col4_edit").hide();
                    $("#row" + i.toString() + "col5_edit").hide();
                    $("#row" + i.toString() + "col6_edit").hide();
                    $("#row" + i.toString() + "col7_edit").hide();
                    
                    $("#row" + i.toString() + "col2").show();
                    $("#row" + i.toString() + "col3").show();
                    $("#row" + i.toString() + "col4").show();
                    $("#row" + i.toString() + "col5").show();
                    $("#row" + i.toString() + "col6").show();
                    $("#row" + i.toString() + "col7").show();
                    
                    //show and hide button
                    $("#row" + i.toString() + "col8 > #bt_update").hide();
                    $("#row" + i.toString() + "col9 > #bt_cancel").hide();
                    $("#row" + i.toString() + "col8 > #bt_edit").show();
                    $("#row" + i.toString() + "col9 > #bt_delete").show();
                    
                    i++;
                }
                
                //can thiet khi xoa item cuoi cung trong 1 page
                if(i == null) i = 0;
                
                //xoa nhung row ko co du lieu
                for(i; i< row_max; i++)
                {
                    $("#row" + i.toString() + "col1").text("");
                    $("#row" + i.toString() + "col2").text("");
                    $("#row" + i.toString() + "col3").text("");
                    $("#row" + i.toString() + "col4").text("");
                    $("#row" + i.toString() + "col5").text("");
                    $("#row" + i.toString() + "col6").text("");
                    $("#row" + i.toString() + "col7").text("");
                    
                    //hide va show cac control va control edit
                    $("#row" + i.toString() + "col2_edit").hide();
                    $("#row" + i.toString() + "col3_edit").hide();
                    $("#row" + i.toString() + "col4_edit").hide();
                    $("#row" + i.toString() + "col5_edit").hide();
                    $("#row" + i.toString() + "col6_edit").hide();
                    $("#row" + i.toString() + "col7_edit").hide();
                    
                    $("#row" + i.toString() + "col2").show();
                    $("#row" + i.toString() + "col3").show();
                    $("#row" + i.toString() + "col4").show();
                    $("#row" + i.toString() + "col5").show();
                    $("#row" + i.toString() + "col6").show();
                    $("#row" + i.toString() + "col7").show();
                    
                    //show and hide button
                    $("#row" + i.toString() + "col8 > #bt_update").hide();
                    $("#row" + i.toString() + "col9 > #bt_cancel").hide();
                    $("#row" + i.toString() + "col8 > #bt_edit").hide();
                    $("#row" + i.toString() + "col9 > #bt_delete").hide();
  
                }
            });
        }
              
        function SortChange(i)
        {
        
            if(i == row_sort)
                sort_desc = !sort_desc;
            else
            {   
                row_sort = i;
                sort_desc = false;
            }
            DropPortChange(1);
        }
        
        function bt_cancel_click(i)
        {
            $("#row" + i.toString() + "col8 > #bt_update").hide();
            $("#row" + i.toString() + "col9 > #bt_cancel").hide();
            $("#row" + i.toString() + "col8 > #bt_edit").show();
            $("#row" + i.toString() + "col9 > #bt_delete").show();
                      
            $("#row" + i.toString() + "col2_edit").hide();
            $("#row" + i.toString() + "col3_edit").hide();
            $("#row" + i.toString() + "col4_edit").hide();
            $("#row" + i.toString() + "col5_edit").hide();
            $("#row" + i.toString() + "col6_edit").hide();
            $("#row" + i.toString() + "col7_edit").hide();
            
            $("#row" + i.toString() + "col2").show();
            $("#row" + i.toString() + "col3").show();
            $("#row" + i.toString() + "col4").show();
            $("#row" + i.toString() + "col5").show();
            $("#row" + i.toString() + "col6").show();
            $("#row" + i.toString() + "col7").show();
        }
        
        function bt_edit_click(i)
        {
            //Get gia tri
            $("#row" + i.toString() + "col2_edit").val($("#row" + i.toString() + "col2").text());
            $("#row" + i.toString() + "col3_edit").val($("#row" + i.toString() + "col3").text());
            $("#row" + i.toString() + "col4_edit").val($("#row" + i.toString() + "col4").text());
            $("#row" + i.toString() + "col5_edit").val($("#row" + i.toString() + "col5").text());
            $("#row" + i.toString() + "col6_edit").val($("#row" + i.toString() + "col6").text());
            $("#row" + i.toString() + "col7_edit").val($("#row" + i.toString() + "col7").text());
            
            //Hide & Show
            $("#row" + i.toString() + "col8 > #bt_edit").hide();
            $("#row" + i.toString() + "col9 > #bt_delete").hide();
            $("#row" + i.toString() + "col8 > #bt_update").show();
            $("#row" + i.toString() + "col9 > #bt_cancel").show();
            
            $("#row" + i.toString() + "col2").hide();
            $("#row" + i.toString() + "col3").hide();
            $("#row" + i.toString() + "col4").hide();
            $("#row" + i.toString() + "col5").hide();
            $("#row" + i.toString() + "col6").hide();
            $("#row" + i.toString() + "col7").hide();
            
            $("#row" + i.toString() + "col2_edit").show();
            $("#row" + i.toString() + "col3_edit").show();
            $("#row" + i.toString() + "col4_edit").show();
            $("#row" + i.toString() + "col5_edit").show();
            $("#row" + i.toString() + "col6_edit").show();
            $("#row" + i.toString() + "col7_edit").show();
        }
        
        function bt_update_click(i)
        {
            var stocktran_id = $("#row" + i.toString() + "col1_edit").val();
            var loaigiaodich = $("#row" + i.toString() + "col2_edit").val();
            var ngay = $("#row" + i.toString() + "col3_edit").val();
            var soluong = $("#row" + i.toString() + "col4_edit").val();
            var gia = $("#row" + i.toString() + "col5_edit").val();
            var phigiaodich = $("#row" + i.toString() + "col6_edit").val();
            var ghichu = $("#row" + i.toString() + "col7_edit").val();
            
            $.getJSON("/Portfolios/UpdatePortTransaction", { stocktran_id:stocktran_id , loaigiaodich:loaigiaodich, ngay:ngay , soluong:soluong , gia:gia , phigiaodich:phigiaodich , ghichu:ghichu }, function(data) {                
                if(data.result == true)
                    DropPortChange(1);
                alert(data.message);
            });
        }
           
        function bt_delete_click(i)
        {
            var stocktran_id = $("#row" + i.toString() + "col1_edit").val();
            $.getJSON("/Portfolios/DeletePortTransaction", {stocktran_id:stocktran_id}, function(data) {  
                DropPortChange(1);
                alert(data.message);
            });
        }

        function UpdatePort()
        {
            var port_id = $('#DropPort').val();
            var tendanhmuc = $('#TB_tendanhmuc').val();
            var phigiaodich = $("#TB_phigiaodich").val();
            
            $.getJSON("/Portfolios/UpdatePort", { port_id:port_id , tendanhmuc:tendanhmuc , phigiaodich:phigiaodich }, function(data) {                
                if(data.result == true)
                {
                    var select = $('#DropPort').val();
                    $('#DropPort').children().remove();
                    $.getJSON("/Portfolios/GetAllPort", null, function(data) {  
                        for(var i in data)
                            if(data[i].Value == select)
                                $("<option value='" + data[i].Value + "' selected='selected'>" + data[i].Text + "</option>").appendTo("#DropPort");             
                            else
                                $("<option value='" + data[i].Value + "'>" + data[i].Text + "</option>").appendTo("#DropPort");             
                    })
                }
                alert(data.message);
            });
        }
    </script>
    <div class="Content_1">
        <div class="Dropdown">
            Tên danh mục&nbsp;&nbsp;
            <select id="DropPort" onchange="DropPortChange(0);"></select>
        </div>
        <div class="Content_1_tab">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tài khoản tiền mặt : 
            <span id="CashName" class="Content_1_tab_normal">123</span>
        </div>
        <div class="Content_1_tab">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Số dư tiền mặt : 
            <span id="AmountMoney" class="Content_1_tab_normal">123</span>
        </div>
    </div>
        
    <div class="Title">
        <%=Html.ActionLink("Basic - Gidview Show", "PortfoliosBasic") %>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <%=Html.ActionLink("Edit  - Gidview Full", "PortfoliosEdit") %>
    </div>
<% 
//Tham so can thiet
int row_max = 7;

//Draw header gridview
Response.Write("<div class='GridView_Header'>");
Response.Write("<span class='GridView_Coulumn_1' onclick='SortChange(1);'>Chứng khoán</span>");
Response.Write("<span class='GridView_Coulumn_2' onclick='SortChange(2);'>Loại giao dịch</span>");
Response.Write("<span class='GridView_Coulumn_3' onclick='SortChange(3);'>Ngày</span>");
Response.Write("<span class='GridView_Coulumn_4' onclick='SortChange(4);'>Số lượng</span>");
Response.Write("<span class='GridView_Coulumn_5' onclick='SortChange(5);'>Giá</span> ");
Response.Write("<span class='GridView_Coulumn_6' onclick='SortChange(6);'>Phí giao dịch</span>");
Response.Write("<span class='GridView_Coulumn_7' onclick='SortChange(7);'>Ghi chú</span> ");
Response.Write("</div>");

//Draw empty row
for (int i = 0; i < row_max; i++)
{
    if (i % 2 == 0)
        Response.Write("<div class='GridView_ItemRow'>");
    else
        Response.Write("<div class='GridView_AlterRow'>");

    Response.Write("<span id='row" + i.ToString() + "col1' class='GridView_Coulumn_1'></span>");
    Response.Write("<span id='row" + i.ToString() + "col2' class='GridView_Coulumn_2'></span>");
    Response.Write("<span id='row" + i.ToString() + "col3' class='GridView_Coulumn_3'></span>");
    Response.Write("<span id='row" + i.ToString() + "col4' class='GridView_Coulumn_4'></span>");
    Response.Write("<span id='row" + i.ToString() + "col5' class='GridView_Coulumn_5'></span>");
    Response.Write("<span id='row" + i.ToString() + "col6' class='GridView_Coulumn_6'></span>");
    Response.Write("<span id='row" + i.ToString() + "col7' class='GridView_Coulumn_7'></span>");

    Response.Write("<input id='row" + i.ToString() + "col1_edit' type='hidden' value=''/>");
    Response.Write("<select id='row" + i.ToString() + "col2_edit' class='GridView_Coulumn_2'><option value='1'>Mua</option><option value='2'>Bán</option></select>");
    Response.Write("<input id='row" + i.ToString() + "col3_edit' class='GridView_Coulumn_3' type='text' />");
    Response.Write("<input id='row" + i.ToString() + "col4_edit' class='GridView_Coulumn_4' type='text' />");
    Response.Write("<input id='row" + i.ToString() + "col5_edit' class='GridView_Coulumn_5' type='text' />");
    Response.Write("<input id='row" + i.ToString() + "col6_edit' class='GridView_Coulumn_6' type='text' />");
    Response.Write("<input id='row" + i.ToString() + "col7_edit' class='GridView_Coulumn_7' type='text' />");

    Response.Write("<span id='row" + i.ToString() + "col8' class='GridView_Coulumn_8'><img id='bt_edit' alt='' src='../../Images/edit.gif' onclick='bt_edit_click(" + i.ToString() + ");' /><img id='bt_update' alt='' src='../../Images/update.gif' onclick='bt_update_click(" + i.ToString() + ");' /></span>");
    Response.Write("<span id='row" + i.ToString() + "col9' class='GridView_Coulumn_9'><img id='bt_delete' alt='' src='../../Images/delete.gif' onclick='bt_delete_click(" + i.ToString() + ");' /><img id='bt_cancel' alt='' src='../../Images/cancel.gif' onclick='bt_cancel_click(" + i.ToString() + ");' /></span>");
    Response.Write("</div>");
}

//Draw footer
Response.Write("<div class='GridView_Footer'>");
Response.Write("Đơn vị : ngàn VNĐ.");
Response.Write("</div>");

//Draw Page
Response.Write("<div class='GridView_Page'>");
Response.Write("<input type='button' value='<' onclick='DropPortChange(2);'></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
Response.Write("<span id='page_current'></span>&nbsp;/&nbsp;");
Response.Write("<span id='page_max'></span>");
Response.Write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='>' onclick='DropPortChange(3);'></span>");
Response.Write("</div>");
%>

    
    <div class="Content_2">
        <div class="FormAdd_Label">
            Tên danh mục
        </div>
        <div class="FormAdd_Data">
            <input id="TB_tendanhmuc" type="text" />
        </div>
        <div class="FormAdd_Label">
            Phi giao dịch
        </div>
        <div class="FormAdd_Data">
            <input id="TB_phigiaodich" type="text" />
        </div>
        <div class="FormAdd_Label">
            <input type="button" value="Lưu thay đổi" onclick="UpdatePort();"/>
        </div>
    </div>
</asp:Content>
